package team.yidong.fenda.wx.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.service.MasterService;
import team.yidong.fenda.wx.Service.WxMasterService;

/**
 * Created by xuanlong on 2017/10/15.
 */


@Controller
public class WxMasterController {
    private final Logger logger = LoggerFactory.getLogger(WxMasterController.class);
    @Autowired
    WxMasterService wxMasterService;
    @Autowired
    MasterService masterService;

    /**
     * 机构的专家列表
     *
     * @param id
     * @return
     */
    @RequestMapping("/wx/findMaster")
    @ResponseBody
    public JSONArray findMaster(@RequestParam Integer id) {
        return wxMasterService.findMaster(id);
    }

    @RequestMapping("/wx/findMasterById")
    @ResponseBody
    public JSONObject findMasterById(Integer masterId) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "master", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList", "answer", "praiseUserSet", "answerList", "question", "masterScoreAnswer", "answerImgSet", "hospital", " masterScore"});
        return JSONObject.fromObject(wxMasterService.findOne(masterId), jsonConfig);
    }

    @RequestMapping("/wx/addMaster")
    @ResponseBody
    public String addMaster(@RequestParam String userName, @RequestParam String password, @RequestParam String login_openId) {

        Jedis jedis = new Jedis("localhost");
        login_openId = jedis.get(login_openId);
        System.out.println("这是我的openId" + login_openId);
        return wxMasterService.addMaster(userName, password, login_openId);
    }

    @RequestMapping("/wx/sumMasterScore")
    @ResponseBody
    public JSONObject sumMasterScore(@RequestParam String openid) {
        Jedis jedis = new Jedis("localhost");
        openid = jedis.get(openid);
        logger.info(openid);
        return wxMasterService.sumMasterScore(openid);
    }

    @RequestMapping("/wx/updateMasterInfo")
    @ResponseBody
    public void wxUpdateMasterInfo(@RequestParam String wxNum, String headImg, String info, String password, String name, String goodAt) {
        Jedis jedis = new Jedis("localhost");
        wxNum = jedis.get(wxNum);
        wxMasterService.updateMasterInfo(wxNum, headImg, info, password, name, goodAt);
    }

    @RequestMapping("/wx/findMasterInfo")
    @ResponseBody
    public JSONObject wxFindMasterInfo(@RequestParam String wxNum) {
        Jedis jedis = new Jedis("localhost");
        wxNum = jedis.get(wxNum);
        return wxMasterService.findMasterInfo(wxNum);
    }

    @RequestMapping("/wx/checkMasterIsBlack")
    @ResponseBody
    public String wxCheckMasterIsBlack(@RequestParam String wxNum) {
        Jedis jedis = new Jedis("localhost");
        wxNum = jedis.get(wxNum);
        return wxMasterService.checkMasterIsBlack(wxNum);
    }

    @RequestMapping("/wx/findMasterByHospitalDepartment")
    @ResponseBody
    public JSONArray findMasterByHospitalDepartment(@RequestParam Integer hospitalDepartmentId) {
        return masterService.findMasterByHospitalDepartmentId(hospitalDepartmentId);
    }
}
